package org.example.main.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.main.entity.House;
import org.example.main.entity.Record;
import org.example.main.service.HouseService;
import org.example.main.service.RecordService;
import org.example.main.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@Tag(name = "历史记录管理",description = "历史记录管理相关接口")
@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private HouseService houseService;

    @Operation(summary = "增加历史记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "增加成功"),
            @ApiResponse(responseCode = "500",description = "增加失败")
    })
    @PostMapping("add")//http://localhost:8089/main/record/add
    public CommonResult add(@RequestBody Record record) {
        CommonResult commonResult = new CommonResult();
        try{
            recordService.add(record);
            commonResult.setCode(200);
            commonResult.setMsg("增加成功");
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg(e.getMessage());
        }
        return commonResult;
    }

    @Operation(summary = "删除历史记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200",description = "删除成功"),
            @ApiResponse(responseCode = "500",description = "删除失败")
    })
    @DeleteMapping("/delete/{recordId}")//http://localhost:8089/main/record/delete/{recordId}
    public CommonResult deleteById(@PathVariable long recordId) {
        CommonResult commonResult = new CommonResult();
        try{
            recordService.deleteById(recordId);
            commonResult.setCode(200);
            commonResult.setMsg("删除成功");
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("删除失败");
        }
        return commonResult;
    }

    @Operation(summary = "查询所有")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询错误")
    })
    @GetMapping("/findAll")//http://localhost:8089/main/record/findAll
    public CommonResult findAll() {
        CommonResult commonResult = new CommonResult();
        try{
            List<Record> records = recordService.findAll();
            commonResult.setCode(200);
            commonResult.setMsg("查询成功");
            commonResult.setData(records);
        }catch (Exception e) {
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据房源ID查询历史记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/house/{houseId}")//http://localhost:8089/main/record/house/{houseId}
    public CommonResult getByHouse(@PathVariable long houseId) {
        CommonResult commonResult = new CommonResult();
        try{
            List<Record> list = recordService.findByHouseId(houseId);
            if (!list.isEmpty()) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(list);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据房东ID查询历史记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/landlord/{landlordId}")//http://localhost:8089/main/record/landlord/{landlordId}
    public CommonResult getByLandlord(@PathVariable long landlordId) {
        CommonResult commonResult = new CommonResult();
        try{
            List<House> houseList = houseService.findByLandlordId(landlordId);
            List<Record> list = new ArrayList<>();
            for (House house : houseList) {
                long currentHouseId = house.getHouseId();
                List<Record> currentHouseRecords = recordService.findByHouseId(currentHouseId);
                list.addAll(currentHouseRecords);
            }
            if (!list.isEmpty()) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(list);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }

    @Operation(summary = "根据租客ID查询历史记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未查询到"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/tenant/{tenantId}")//http://localhost:8089/main/record/tenant/{tenantId}
    public CommonResult getByTenant(@PathVariable long tenantId) {
        CommonResult commonResult = new CommonResult();
        try{
            List<Record> list = recordService.findByTenantId(tenantId);
            if (!list.isEmpty()) {
                commonResult.setCode(200);
                commonResult.setMsg("查询成功");
                commonResult.setData(list);
            }else{
                commonResult.setCode(404);
                commonResult.setMsg("未查询到");
            }
        }catch (Exception e){
            commonResult.setCode(500);
            commonResult.setMsg("查询失败");
        }
        return commonResult;
    }
}
